package com.bz.xtcx.manager.mapper;

import com.bz.xtcx.manager.mapper.provider.DataStatisticsProvider;
import com.bz.xtcx.manager.vo.VoTaskSum;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;
import java.util.Map;

public interface DataStatisticsMapper {
	
	@Select("select count(*) from ${tableName} ")
	int count(@Param("tableName")String tableName);
	
	//%m	月，数值(00-12)
	@Select("select date_format(create_time ,'%Y-%c' ) date,count(*) count from ${tableName} where create_time >= #{date} group by date_format(create_time ,'%Y-%c' ) ")
	List<Map<String, Object>> groupByMonth(@Param("tableName")String tableName, @Param("date")String date);
	
	@Select("select user_type type, count(*) count from `bus_user` group by user_type")
	List<Map<String, Object>> groupByUserType();
	
	@Select("select JSON_EXTRACT(NULLIF(country,''),'$[0]') country, count(*) count from ${tableName} where NULLIF(country,'')!='' group by JSON_EXTRACT(NULLIF(country,''),'$[0]')")
	List<Map<String, Object>> groupByCountry0(@Param("tableName")String tableName);
	
	@Select("select JSON_EXTRACT(NULLIF(country,''),'$[1]') country, count(*) count from ${tableName} where JSON_EXTRACT(NULLIF(country,''),'$[0]') = #{country} group by JSON_EXTRACT(NULLIF(country,''),'$[1]')")
	List<Map<String, Object>> groupByCountry1(@Param("tableName")String tableName, @Param("country")String country);
	
	@Select("select JSON_EXTRACT(NULLIF(country,''),'$[2]') country, count(*) count from ${tableName} where JSON_EXTRACT(NULLIF(country,''),'$[1]') = #{country} group by JSON_EXTRACT(NULLIF(country,''),'$[2]')")
	List<Map<String, Object>> groupByCountry2(@Param("tableName")String tableName, @Param("country")String country);
	
	@Select("select ${type} type, count(*) count from ${tableName} group by ${type}")
	List<Map<String, Object>> groupByType(@Param("tableName")String tableName, @Param("type")String type);
	
	@Select("select type_id formType, check_status checkStatus, count(*) count from `cms_meeting` where fromer=#{userId} group by check_status,type_id")
	List<VoTaskSum> findMeetingFromer(@Param("userId")String userId);
	
	@Select("select type_id formType, check_status checkStatus, count(*) count from `cms_meeting` where targeter=#{userId} and check_status=0  group by check_status,type_id")
	List<VoTaskSum> findMeetingTargeter(@Param("userId")String userId);
	
	@Select("select a.* from (select ${queryField} from ${tableName} where 1=1 ${queryCond} group by ${groupBy}) a")
	List<Map<String, Object>> commonGroupBy(@Param("tableName")String tableName, @Param("queryField")String queryField, @Param("queryCond")String queryCond, @Param("groupBy")String groupBy);

	@Select("select count(*) count from `cms_meeting`")
	int countAllMeeting();

    @Select("select count(*) count from `cms_meeting` where check_status = 1")
    int countAllSuccessMeeting();

    @Select("select count(*) count, type_id formType from `cms_meeting` group by type_id")
    List<VoTaskSum> countAllMeetingByType();

    @Select("select count(*) count, type_id formType from `cms_meeting` where check_status = 1 group by type_id")
    List<VoTaskSum> countAllSuccessMeetingByType();

    // add by wangtao on 20190103 for 对接统计增加时间区间条件 start
    @SelectProvider(type = DataStatisticsProvider.class, method = "countAllMeetingByDatetimeProvider")
    int countAllMeetingByDatetime(String startDateTime, String endDateTime);

    @SelectProvider(type = DataStatisticsProvider.class, method = "countAllSuccessMeetingByDatetimeProvider")
    int countAllSuccessMeetingByDatetime(String startDateTime, String endDateTime);

    @SelectProvider(type = DataStatisticsProvider.class, method = "countAllMeetingByTypeAndDatetimeProvider")
    List<VoTaskSum> countAllMeetingByTypeAndDatetime(String startDateTime, String endDateTime);

    @SelectProvider(type = DataStatisticsProvider.class, method = "countAllSuccessMeetingByTypeAndDatetimeProvider")
    List<VoTaskSum> countAllSuccessMeetingByTypeAndDatetime(String startDateTime, String endDateTime);
    // add by wangtao on 20190103 for 对接统计增加时间区间条件 end

    @Select("select count(*) count from `index_visiter`")
    int countAllVisiter();

    // add by wangtao on 2190103 for 增加按月统计访问量 start
	@Select("select date_format(visit_time,'%Y-%m') gpdate, count(*) count from `index_visiter` group by date_format(visit_time,'%Y-%m' ) order by gpdate desc")
    List<Map<String, Integer>> countVisiterByMonth();
    // add by wangtao on 2190103 for 增加按月统计访问量 end
}
